package com.slipper.system.ai.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.ai.dao.AiApplicationDao;
import com.slipper.system.ai.bo.AiApplicationBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class AiApplicationService {
    @Resource
    private AiApplicationDao aiApplicationDao;
    
    public AiApplicationBO queryBy(String id) {
        return aiApplicationDao.queryBy(id);
    }
    
    public List<AiApplicationBO> queryList(AiApplicationBO bo) {
        return aiApplicationDao.queryList(bo);
    }
    
    public PageInfo<AiApplicationBO> queryPage(AiApplicationBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(aiApplicationDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(AiApplicationBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return aiApplicationDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return aiApplicationDao.insert(bo);
        }
    }

    @WriteTransactional
    public void saveList(List<AiApplicationBO> list) {
        list.forEach(item -> {
            if (aiApplicationDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                aiApplicationDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                aiApplicationDao.insert(item);
            }
        });
    }
    
    @WriteTransactional
    public int delete(AiApplicationBO bo) {
        return aiApplicationDao.delete(bo);
    }

    public List<AiApplicationBO> queryBySystemId(AiApplicationBO bo) {
        return aiApplicationDao.queryBySystemId(bo);
    }
}
